package com.zycfc.zsf.boot.mq.rabbitmq.converter;

import java.lang.reflect.*;
import org.springframework.amqp.support.converter.*;
import org.springframework.amqp.core.*;
import java.io.*;
import org.slf4j.*;
import com.fasterxml.jackson.databind.*;

public class JsonMessageConverter extends AbstractMessageConverter
{
    private static final Logger log;
    private static final Logger logger;
    public static final String DEFAULT_CHARSET = "UTF-8";
    private static final SimpleMessageConverter SIMPLE_MESSAGE_CONVERTER;
    private static final ObjectMapper jsonObjectMapper;
    private final JavaType javaType;
    private boolean autoGetAmqpMessage;
    
    public JsonMessageConverter(final Type type) {
        this.autoGetAmqpMessage = false;
        if (null != type) {
            this.javaType = JsonMessageConverter.jsonObjectMapper.getTypeFactory().constructType(type);
        }
        else {
            this.javaType = null;
        }
    }
    
    public Object fromMessage(final Message message) throws MessageConversionException {
        Object content = null;
        final MessageProperties properties = message.getMessageProperties();
        String contentType = null;
        if (properties != null) {
            contentType = properties.getContentType();
        }
        Label_0165: {
            if (contentType != null) {
                if (contentType.equals("application/json")) {
                    try {
                        final String json = new String(message.getBody(), "UTF-8");
                        if (JsonMessageConverter.logger.isDebugEnabled()) {
                            JsonMessageConverter.logger.debug("\u6d88\u606f\u62a5\u6587\u4fe1\u606f:{}", (Object)json);
                        }
                        content = this.jsonToObject(json);
                        break Label_0165;
                    }
                    catch (Exception e) {
                        throw new MessageConversionException("failed to convert serialized Message content", (Throwable)e);
                    }
                }
                if (contentType.equals("text/plain")) {
                    try {
                        final String msg = new String(message.getBody(), "UTF-8");
                        if (JsonMessageConverter.logger.isDebugEnabled()) {
                            JsonMessageConverter.logger.debug("the text/plain fromMessage:{}", (Object)msg);
                        }
                        content = msg;
                    }
                    catch (Exception e) {
                        throw new MessageConversionException("failed to convert serialized Message content", (Throwable)e);
                    }
                }
            }
        }
        if (content == null) {
            content = message.getBody();
        }
        return content;
    }
    
    public static String getMessageContent(final Message message) {
        String content = "\u4e0d\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u6d88\u606f";
        final MessageProperties properties = message.getMessageProperties();
        String contentType = null;
        if (properties != null) {
            contentType = properties.getContentType();
        }
        if (contentType != null) {
            if (contentType.equals("application/json")) {
                try {
                    content = new String(message.getBody(), "UTF-8");
                }
                catch (UnsupportedEncodingException e) {
                    JsonMessageConverter.logger.error(e.getMessage(), (Throwable)e);
                }
            }
            else if (contentType.startsWith("text")) {
                String encoding = properties.getContentEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                try {
                    content = new String(message.getBody(), encoding);
                }
                catch (UnsupportedEncodingException e2) {
                    JsonMessageConverter.logger.error(e2.getMessage(), (Throwable)e2);
                }
            }
        }
        return content;
    }
    
    public Object jsonToObject(final String json) throws Exception {
        Object object = null;
        if (json != null && json.trim().length() > 0) {
            if (null == this.javaType) {
                JsonMessageConverter.log.info("javaType=null");
                throw new Exception(" type is null");
            }
            if (JsonMessageConverter.logger.isDebugEnabled()) {
                JsonMessageConverter.log.debug("javaType={}", (Object)this.javaType.getTypeName());
            }
            object = JsonMessageConverter.jsonObjectMapper.readValue(json, this.javaType);
        }
        return object;
    }
    
    protected Message createMessage(final Object object, final MessageProperties messageProperties) throws MessageConversionException {
        byte[] bytes = null;
        Label_0162: {
            if (object instanceof byte[]) {
                bytes = (byte[])object;
                messageProperties.setContentType("application/octet-stream");
            }
            else {
                if (!(object instanceof String)) {
                    final String contentType = messageProperties.getContentType();
                    if (null != contentType && !"application/json".equals(contentType)) {
                        if (!"application/octet-stream".equals(contentType)) {
                            return JsonMessageConverter.SIMPLE_MESSAGE_CONVERTER.toMessage(object, messageProperties);
                        }
                    }
                    try {
                        final String json = JsonMessageConverter.jsonObjectMapper.writeValueAsString(object);
                        bytes = json.getBytes("UTF-8");
                        messageProperties.setContentType("application/json");
                        messageProperties.setContentEncoding("UTF-8");
                        break Label_0162;
                    }
                    catch (Exception e) {
                        throw new MessageConversionException("failed to convert to serialized Message content", (Throwable)e);
                    }
                    return JsonMessageConverter.SIMPLE_MESSAGE_CONVERTER.toMessage(object, messageProperties);
                }
                try {
                    bytes = ((String)object).getBytes("UTF-8");
                }
                catch (UnsupportedEncodingException e2) {
                    throw new MessageConversionException("failed to convert to Message content", (Throwable)e2);
                }
                messageProperties.setContentType("text/plain");
                messageProperties.setContentEncoding("UTF-8");
            }
        }
        if (bytes != null) {
            messageProperties.setContentLength((long)bytes.length);
        }
        return new Message(bytes, messageProperties);
    }
    
    public void setAutoGetAmqpMessage(final boolean autoGetAmqpMessage) {
        this.autoGetAmqpMessage = autoGetAmqpMessage;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)JsonMessageConverter.class);
        logger = LoggerFactory.getLogger((Class)JsonMessageConverter.class);
        SIMPLE_MESSAGE_CONVERTER = new SimpleMessageConverter();
        (jsonObjectMapper = new ObjectMapper()).configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        JsonMessageConverter.jsonObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
